//
//  WD_BulletManager.m
//  BUDING
//
//  Created by HanWoonYong on 16/4/12.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_BulletManager.h"
#import "WD_BulletView.h"
@interface WD_BulletManager()
@property (nonatomic, strong)NSMutableArray *allContent;// 所有评论数组
@property (nonatomic, strong)NSMutableArray *hylContent;// 管理数组
@property (nonatomic, strong)NSMutableArray *bulletQueue;// 弹幕数组
@property (nonatomic, assign)BOOL kStart;
@property (nonatomic, assign)BOOL kStop;
@end

@implementation WD_BulletManager

- (void)start {
    if (self.hylContent.count == 0) {
        [self.hylContent addObjectsFromArray:self.allContent];
    }
    self.kStart = YES;
    self.kStop = NO;
    [self initBulletView];
}



- (void)stop {
    self.kStop = YES;
    [self.bulletQueue enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        WD_BulletView *bulletView = obj;
        [bulletView removeFromSuperview];
        bulletView = nil;
    }];
}

#pragma mark - 导弹准备
/**
 *  content 评论内容
 *  trajectory 导弹弹道
 **/
- (void)creatBulletContent:(NSString *)content trajectory:(WDTrajectory)trajectory {
    if (self.kStop) {
        return;
    }
    // 创建一个弹幕
    WD_BulletView *bulletView = [[WD_BulletView alloc] initWithContent:content];
    
    // 设置运行轨道
    bulletView.trajectory = trajectory;
    __weak WD_BulletView *kView = bulletView;
    __weak WD_BulletManager *mySelf = self;
    
    
    kView.fireBlock = ^(WDBulletMove state) {
        if (mySelf.kStop) {
            return ;
        }
        switch (state) {
            case Start:
                // 把弹幕加入弹幕数组中
                [self.bulletQueue addObject:kView];
                break;
            case Enter:{
                NSString *content = [mySelf nextBullet];
                if (content) {
                    [mySelf creatBulletContent:content trajectory:trajectory];
                } else{
                    // 没弹幕了
                }
                break;
            }
            case End:{
                // 弹幕飞出屏幕后从管理中删除
                if ([mySelf.hylContent containsObject:kView]) {
                    [mySelf.hylContent removeObject:kView];
                }
                if (mySelf.hylContent.count == 0) {
                    //                    [mySelf start];// 没弹幕就循环
                }
                break;
            }
            default:
                break;
        }
    };
    if (self.FireBulletBlock) {
        self.FireBulletBlock(kView);
    }
}

#pragma mark - 初始化弹幕
- (void)initBulletView {
    // 初始化三条弹道
    NSMutableArray *bulletArr = [NSMutableArray arrayWithArray:@[@(0), @(1), @(2)]];
    for (int i = 3; i > 0; i--) {
        NSString *centent = [self.hylContent firstObject];
        if (centent) {
            [self.hylContent removeObjectAtIndex:0];
            NSInteger index = arc4random()%bulletArr.count;// 随机生成弹道创建弹幕
            WDTrajectory trajectory = [[bulletArr objectAtIndex:index] intValue];
            [bulletArr removeObjectAtIndex:index];
            [self creatBulletContent:centent trajectory:trajectory];
        } else {
            // 当小于三个，则退出
            break;
        }
    }
}

#pragma mark - 下一个弹幕准备
- (NSString *)nextBullet {
    NSString *content = [self.hylContent firstObject];
    if (content) {
        [self.hylContent removeObjectAtIndex:0];
    }
    return content;
}

- (NSMutableArray *)allContent {
    if (!_allContent) {
        _allContent = [NSMutableArray arrayWithArray:self.bulletArr];
    }
    return _allContent;
}

- (NSMutableArray *)hylContent {
    if (!_hylContent) {
        _hylContent = [NSMutableArray array];
    }
    return _hylContent;
}

- (NSMutableArray *)bulletQueue {
    if (!_bulletQueue) {
        _bulletQueue = [NSMutableArray array];
    }
    return _bulletQueue;
}


@end
